package com.junyue.control.platform.utils;

import com.alibaba.fastjson.JSONArray;
import com.junyue.control.platform.PlatformApplication;
import com.junyue.control.platform.entity.ConfigBeanValue;
import com.junyue.control.platform.entity.ControlData;
import com.junyue.control.platform.entity.ResultData;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class FileLoader {
    private static FileLoader fileLoader = null;
    private Map<String, ResultData> dataMap;

    public Map<String, ResultData> getDataMap() {
        return this.dataMap;
    }

    public void setDataMap(Map<String, ResultData> dataMap) {
        this.dataMap = dataMap;
    }

    private FileLoader(String filePath) {
        this.dataMap = new HashMap();
        String data = ReadFileUtils.readFileByLines(filePath);
        String temp = data.replaceAll("\t", "");
        List<ControlData> controlDataList = JSONArray.parseArray(temp, ControlData.class);
        for (ControlData controlData : controlDataList) {
            this.dataMap.put(controlData.getVersionName(), controlData.getResultData());
        }
    }

    public static FileLoader getInstance(String filePath) {
        if (fileLoader == null) {
            fileLoader = new FileLoader(filePath);
            return fileLoader;
        } else
            return fileLoader;
    }
}
